package cn.tedu.egnotice1014.controller;

import cn.tedu.egnotice1014.base.response.JsonResult;
import cn.tedu.egnotice1014.base.response.StatusCode;
import cn.tedu.egnotice1014.mapper.NoticeMapper;
import cn.tedu.egnotice1014.pojo.dto.NoticeAddParam;
import cn.tedu.egnotice1014.pojo.dto.NoticeQuery;
import cn.tedu.egnotice1014.pojo.entity.Notice;
import cn.tedu.egnotice1014.pojo.vo.NoticeListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/notice/")
@Slf4j
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("add")
    public JsonResult addNotice(@RequestBody NoticeAddParam param){
        log.debug("param:"+param);
        Notice notice=new Notice();
        BeanUtils.copyProperties(param,notice);
        notice.setUserId(1);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        log.debug("notice:"+notice);
        noticeMapper.insertNotice(notice);
        int rows=noticeMapper.insertNotice(notice);
        if (rows>0){
            return JsonResult.OK();
        }else {
            return new JsonResult(StatusCode.UPERATION_FAILED);
        }
    }
    @GetMapping("list")
    public JsonResult listNotice(NoticeQuery  query){
        log.debug("query:"+query);
        List<NoticeListVO> noticeListVO=noticeMapper.getNoticeList(query);
        return JsonResult.OK(noticeListVO);
    }



}
